package com.fast.controller.Async;

import lombok.extern.slf4j.Slf4j;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Future;

/**
 * @Author 乔博宇
 * @Date 2021/7/26
 * @Description
 */
@Service
@Slf4j
public class SyncService {

    // AsyncResult 返回类 import java.util.concurrent.Future;
    @Async // 异步调用注解 java8之后
    public Future<String> newThread(LocalDateTime start) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalDateTime end = LocalDateTime.now();
        log.info("异步线程执行的时间为:" + Duration.between(start, end).toMillis());
        return new AsyncResult<String>("我是异步线程返回的结果");
    }

}
